<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="220px" class="admin-aside">
      <div class="logo">
        <h2>管理后台</h2>
      </div>
      
      <el-menu
        :default-active="currentRoute"
        class="admin-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        router>
        
        <el-menu-item index="/admin/dashboard">
          <el-icon><Monitor /></el-icon>
          <span>仪表板</span>
        </el-menu-item>

        <el-menu-item index="/admin/users">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/stores">
          <el-icon><Shop /></el-icon>
          <span>商家管理</span>
          <el-badge v-if="pendingStoresCount > 0" :value="pendingStoresCount" class="badge" />
        </el-menu-item>

        <el-menu-item index="/admin/takeouts">
          <el-icon><Food /></el-icon>
          <span>外卖管理</span>
          <el-badge v-if="pendingTakeoutsCount > 0" :value="pendingTakeoutsCount" class="badge" />
        </el-menu-item>

        <el-menu-item index="/admin/orders">
          <el-icon><Document /></el-icon>
          <span>订单管理</span>
        </el-menu-item>

        <el-menu-item index="/admin/statistics">
          <el-icon><DataAnalysis /></el-icon>
          <span>数据统计</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container>
      <!-- 头部 -->
      <el-header height="60px" class="admin-header">
        <div class="header-left">
          <span class="welcome-text">欢迎，{{ username }}</span>
        </div>
        <div class="header-right">
          <el-button text @click="goToHome">
            <el-icon><House /></el-icon>
            <span>前台首页</span>
          </el-button>
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              <el-avatar :src="headPortrait" />
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区 -->
      <el-main class="admin-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Monitor, User, Shop, Food, Document, DataAnalysis, 
  House, ArrowDown 
} from '@element-plus/icons-vue'
import { userInfoStore } from '@/assets/userStore/userStore'
import { adminApis } from '@/assets/apis/AdminApis'

const route = useRoute()
const router = useRouter()
const userStore = userInfoStore()

const currentRoute = computed(() => route.path)
const username = computed(() => userStore.username)
const headPortrait = computed(() => userStore.head_portrait)

// 待审核数量
const pendingStoresCount = ref(0)
const pendingTakeoutsCount = ref(0)

// 获取待审核数量
const fetchPendingCount = async () => {
  try {
    const adminId = userStore.id
    
    // 获取待审核商家
    const storesData = await adminApis.getPendingStores(adminId)
    pendingStoresCount.value = storesData.total || 0
    
    // 获取待审核外卖
    const takeoutsData = await adminApis.getPendingTakeouts(adminId)
    pendingTakeoutsCount.value = takeoutsData.total || 0
  } catch (error) {
    console.error('获取待审核数量失败：', error)
  }
}

// 返回前台首页
const goToHome = () => {
  router.push('/')
}

// 下拉菜单操作
const handleCommand = (command: string) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 清空用户信息
      userStore.$reset()
      ElMessage.success('已退出登录')
      router.push('/admin/login')
    }).catch(() => {})
  }
}

onMounted(() => {
  fetchPendingCount()
  // 每30秒刷新一次待审核数量
  setInterval(fetchPendingCount, 30000)
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.admin-aside {
  background-color: #304156;
  overflow-x: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  border-bottom: 1px solid #1f2d3d;
}

.logo h2 {
  margin: 0;
  font-size: 20px;
}

.admin-menu {
  border: none;
}

.admin-menu .el-menu-item {
  position: relative;
}

.badge {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}

.admin-header {
  background: white;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.header-left .welcome-text {
  font-size: 14px;
  color: #606266;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.user-dropdown:hover {
  opacity: 0.8;
}

.admin-main {
  background: #f0f2f5;
  padding: 20px;
}

:deep(.el-menu-item.is-active) {
  background-color: rgba(64, 158, 255, 0.1) !important;
}
</style>

